package org.shopen.ai.puremvc.view.domain.cities
{
	import flash.display.DisplayObject;
	
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.model.domain.cities.CountryProxy;
	import org.shopen.ai.puremvc.view.domain.cities.components.SelectCountryWindow;
	import org.shopen.ai.puremvc.view.domain.cities.events.SelectNodeEvent;
	import org.un.cava.birdeye.ravis.graphLayout.data.IGraph;

	public class SelectCountryWindowMediator extends Mediator
	{
		public static const NAME : String = "SelectCountryWindowMediator";
		
		protected function get parent() : DisplayObject
		{
			var result : DisplayObject = viewComponent as DisplayObject;
			return result;
		}
		
		protected var id : String = "";
		protected var countryWindow : SelectCountryWindow = null;
		
		public function SelectCountryWindowMediator(id : String, viewComponent:Object=null)
		{
			super(SelectCountryWindowMediator.NAME + id, viewComponent);
			this.id = id;
		}
		
		override public function getMediatorName():String
		{
			return SelectCountryWindowMediator.NAME + id;
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationConstants.SHOW_SELECTCOUNTRY,
					CountryProxy.COUNTRY_GRAPH_LOADED, 
					CountryProxy.COUNTRY_GRAPH_LOAD_FAILED];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var type : String = notification.getType();
			if (id != type)
			{
				return;
			}
			var name : String = notification.getName();
			switch (name)
			{
				case ApplicationConstants.SHOW_SELECTCOUNTRY:
					showSelectCountryWindow();
					break;
				case CountryProxy.COUNTRY_GRAPH_LOADED:
					var graph : IGraph = notification.getBody() as IGraph;
					countryWindow.graph = graph;
					break;
				case CountryProxy.COUNTRY_GRAPH_LOAD_FAILED:
					sendNotification(ApplicationConstants.GENERAL_ERROR, "Load map data failed!");
					break;
			}
		}
		
		protected function showSelectCountryWindow() : void
		{
			// Make sure only one per type select country window is shown
			if (countryWindow == null)
			{
				countryWindow = createWindow();
				PopUpManager.addPopUp(countryWindow, parent, true);
			}
			
			PopUpManager.bringToFront(countryWindow);
			PopUpManager.centerPopUp(countryWindow);
		}
		
		protected function createWindow() : SelectCountryWindow
		{
			var window : SelectCountryWindow = new SelectCountryWindow();
			
			window.addEventListener(CloseEvent.CLOSE, windowCloseListener);
			window.addEventListener(FlexEvent.CREATION_COMPLETE, windowCreationCompleteListener);
			window.addEventListener(SelectNodeEvent.SELECT_NODE, selectNodeListener);
			
			return window;
		}
		
		private function windowCloseListener(event : CloseEvent) : void
		{
			if (countryWindow == event.target)
			{
				removeCloseWindow();
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		protected function removeCloseWindow() : void
		{
			countryWindow.removeEventListener(CloseEvent.CLOSE, windowCloseListener);
			countryWindow.removeEventListener(FlexEvent.CREATION_COMPLETE, windowCreationCompleteListener);
			countryWindow.removeEventListener(SelectNodeEvent.SELECT_NODE, selectNodeListener);
			PopUpManager.removePopUp(countryWindow);
			countryWindow = null;
		}
		
		private function windowCreationCompleteListener(event : FlexEvent) : void
		{
			sendNotification(ApplicationConstants.LOAD_COUNTRY_GRAPH, null, id); 
		}
		
		private function selectNodeListener(event : SelectNodeEvent) : void
		{
			removeCloseWindow();
			sendNotification(ApplicationConstants.CITY_SELECTED, event.node, id);
		}
		
	}
}